import java.awt.Polygon;
import java.util.ArrayList;

/*
 * Created by Jesse Phillips
 * Created on 12-Jan-06
 */
public class Triangle extends TwoDShape {
	protected double a,b,c;

	protected Triangle(double a, double b, double c) {
		super("Triangle");
		this.a = a;
		this.b = b;
		this.c = c;
	}

	@Override
	public double area() {
		double cosA = (b*b + c*c - a*a)/(2*b*c);
		double height = Math.acos(cosA);
		
		return b/2*height;
	}

	@Override
	public double perimeter() {
		return a+b+c;
	}
	
	public static String validate(double a, double b, double c) {
		double[] allSides = {a, b, c};
		int longest = 0;
		
		for(int i = 0; i < allSides.length; i++)
			if(!TwoDShape.validate(allSides[i]))
				return "Sides must be greater than zero.";
		
		//find out if shorter sides are less then longest.
		if(a <= b)
			longest = 1;
		if(b <= c)
			longest = 2;
		double shortTotal = 0;
		for(int i = 0; i < allSides.length; i++)
			if(i != longest)
				shortTotal += allSides[i];
		
		if(shortTotal < allSides[longest])
			return "One side is too long to allow other sides to reach eachother.";
		
		return null;
	}
	
	public static Triangle create(double a, double b, double c) throws ShapeException {
		String error = validate(a,b,c);
		if(error == null)
			return new Triangle(a,b,c);
		else {
			ArrayList<Double> foo = new ArrayList<Double>();
			foo.add(a);
			foo.add(b);
			foo.add(c);
			throw new ShapeException("Triangle", error, foo);
		}
	}
	
	public String toString() {
		return super.toString()+"\nSides: "+a+", "+b+", "+c;
	}
}
